#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Osh.Media;

namespace Osh.ProgDvb
{
	/// <summary>
	/// Summary description for ChannelTreeFolder.
	/// </summary>
	public class ChannelFolder : MediaFolder
	{
		public ChannelFolder(ChannelTree channelTree, BinaryReader reader, string id) : base(channelTree, id, string.Empty)
		{
			int channelIdSize = channelTree.ChannelIdSize;

			uint folderSize = reader.ReadUInt32();
			this.Name = reader.ReadString(64);

			uint channelCount = reader.ReadUInt32();

			byte[] channelIdBytes = new byte[ChannelId.Size];
			for(uint i = 0; i < channelCount; i++)
			{
				long position = reader.BaseStream.Position;

				reader.Read(channelIdBytes, 0, channelIdSize);
				ChannelId channelId = new ChannelId(channelIdBytes);

				Channel channel;
				if((this.Tree as ChannelTree).ChannelDatabase.TryGetChannel(channelId, out channel))
				{
					this.ItemList.Add(channel);
					channel.AddFolder(this);
				}
				reader.BaseStream.Position = position + channelIdSize;
			}
		}
	}
}
